// SIPStack.h: interface for the CSIPStack class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SIPSTACK_H__80E56D7C_98BF_414E_BEF1_0F7E52763237__INCLUDED_)
#define AFX_SIPSTACK_H__80E56D7C_98BF_414E_BEF1_0F7E52763237__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "public.h"
#include "mystring.h"
#include <map>
#include "Transaction.h"
#include "Transport.h"
#include "SIPMessage.h"
#include "Fifo.h"
#include "MethodTypes.h"
#include "base.h"

#define SUPPORTED_SCHEMES	4
#define CLEARLIST(l,t) t *pt;while(l.empty() == false){pt = l.front();l.pop_front();delete pt;};
#define COPYLIST(s,d,t) list<t*>::const_iterator i;for(i=s.begin();i!=s.end();i++){d.push_back(new t(**i));}

//using namespace std;

struct NATAddress{
	mystring	ip;
	uint16		port;
};
struct PresenceAgentAddress{
	mystring	ip;
	uint16		port;
};
class BASE_API CSIPStack  
{
public:
	//yxy:0410
	//void SetClientVideoDialog(	CSIPDialog*	pdialog){m_pClientVideoDialog = pdialog;};
//	void SetClientVideoDialog(	CSIPDialog*	pdialog){if(pdialog) m_pClientDialogPool.push_back(pdialog);};
	//void RmvClientVideoDialog(CSIPDialog* pdialog){if(pdialog) m_pClientDialogPool.remove(pdialog);};
	bool UDPNATExpiresSend(string & ip_address, uint16 port);
	bool SendResponse(CSIPMessage &msg);
	void SetTUMsgFifo(CFifo<CSIPMessage> *pfifo){m_pTUMsgFifo = pfifo;};
	void SendRequest(CSIPMessage &msg);

	void AddtoTUFifo(CSIPMessage *pmsg);
	int ProcessTransport();
	
	int ProcessTransaction();
	void SetRegistrar(bool r){m_bRegistrar = r;};	
	void SetNATExpires(int natexpires);	
	void SetUserAgent(const char *pua){m_strUserAgent = pua;};
	void SetNATType(int type){m_nNATType = type;};
	bool AddTransport( CTransport::TYPE protocol, 
                        int port,
                        const char* hostName = "");
	int TransportSendResponse(CSIPMessage &response);
	int	GetUDPPort();
	int GetTCPPort();
	int TransportSendRequest(CSIPMessage &request);
	int ProxyTransportSendRequest(CSIPMessage &request);
	void TransportAddMsg(CSIPMessage *pmsg){m_TransportMsgFifo.AddAndSet(pmsg);}//yb 20100118 changed from m_TransportMsgFifo.Add()
    int   TransportFifoSize(){return m_TransportMsgFifo.Size();}
	static CSIPMessage * MakeACK(CSIPMessage& request,CSIPMessage &response);
	static CSIPMessage* MakeRequest(const CSIPNameAddr& target, const CSIPNameAddr& from, const CSIPNameAddr& contact, MethodTypes method);
	static CSIPMessage* MakeResponse(const CSIPMessage& request, int responseCode, const char * reason=NULL);
	static CSIPMessage* MakeResponse(const CSIPMessage& request, int responseCode, const CSIPNameAddr& contact,  const char* reason);

	static CSIPMessage* MakeCancel(const CSIPMessage& request);
	
	mystring GetBranch();
	mystring GetTag();
	mystring GetLocalAddress();

	void SetLocalAddress(const char *pip){m_strLocalAddress = pip;};

	void SetUserName(const char *pusername){m_strUserName = pusername;};
	void SetHostName(const char *phostname);
	CSIPStack();
	virtual ~CSIPStack();
	static CLock				m_CallIDLock;
protected:
	int RequestValidation(const CSIPMessage *pmsg);
	void AddTransaction(CSIPMessage *pmsg);
	mystring ComputeBranch(const CSIPMessage *pmsg);
	bool IsSupportedScheme(const mystring &scheme);
	
	//void NotifyPresence(mystring aor,mystring state,CContacts &cs);
protected:

	unsigned long		m_nBranchCount;
	unsigned long		m_nTagCount;
	unsigned long		m_nBranchStart;
	mystring			m_strHostName;
	mystring			m_strUserAgent;
	static mystring		m_strSchemes[SUPPORTED_SCHEMES];
//	mystring			m_strServerAddress;
	mystring			m_strLocalAddress;
	mystring			m_strUserName;
	mystring			m_strPassword;
	CLock				m_BranchLock;
	CLock				m_TagLock;	
	
	bool				m_bRegistrar;
	int					m_nNATType;
	std::map<mystring, CTransaction*>	m_ServerTransactionMap;
    CLock                               m_ServerTransactionMapLocker;//yb added 20100118 for sync m_ServerTransactionMap
	std::map<mystring, CTransaction*>	m_ClientTransactionMap;
    CLock                               m_ClientTransactionMapLocker;//yb added 20091231 for sync m_ClientTransactionMap
	std::map<mystring, CTransport*>	m_TransportMap;
	CFifo<CSIPMessage>*			m_pTUMsgFifo;

	CFifo<CSIPMessage>				m_TransportMsgFifo;

	CFifo<CSIPMessage>				m_CoreMsgFifo;
//	CSIPDialog*				m_pClientVideoDialog;
	//yxy:0410 
//	list<CSIPDialog*>       m_pClientDialogPool;
	//yxy:0410 end
//	CLock	m_Lock;
	

};

#endif // !defined(AFX_SIPSTACK_H__80E56D7C_98BF_414E_BEF1_0F7E52763237__INCLUDED_)
